package apptoko.com.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;
import apptoko.com.R;

public class UserManualView extends SherlockFragmentActivity {
	String url;
	Intent callbackIntent;
		@Override
		public void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.mywebview);
			getSupportActionBar().setDisplayHomeAsUpEnabled(true);
			getSupportActionBar().setTitle("Apptoko "+getResources().getString(R.string.user_manual));
			callbackIntent=getIntent();
			Bundle extras = callbackIntent.getExtras();
			if (extras != null) {
				url = extras.getString("url");
			}
			WebView webview = (WebView) findViewById(R.id.myWeb);
			webview.requestFocus(View.FOCUS_DOWN);
			webview.getSettings().setJavaScriptEnabled(true);
			webview.setWebViewClient( new WebViewClient()
	        {
	            @Override
	            public boolean shouldOverrideUrlLoading(WebView view, String url)
	            {
	            	if(url.startsWith("mailto:")){
	                    Intent i = newEmailIntent(UserManualView.this, extractEmail(url), "Hi Apptoko", "Hi Apptoko,");
	                    startActivity(i);
	                    view.reload();
	                    return true;
	                }
	            	view.loadUrl (url); 
	                return true;
	            }
	        });
			webview.loadUrl("file://"+url);
	    }
		public static Intent newEmailIntent(Context context, String address, String subject, String body) {
		      Intent intent = new Intent(Intent.ACTION_SEND);
		      intent.putExtra(Intent.EXTRA_EMAIL, new String[] { address });
		      intent.putExtra(Intent.EXTRA_TEXT, body);
		      intent.putExtra(Intent.EXTRA_SUBJECT, subject);
//		      intent.putExtra(Intent.EXTRA_CC, cc);
		      intent.setType("message/rfc822");
		      return intent;
	}
		@Override
		public boolean onOptionsItemSelected(MenuItem item) {
			// TODO Auto-generated method stub
			switch (item.getItemId()) {
			case android.R.id.home:
				finish();
				return true;
			}
			return super.onOptionsItemSelected(item);
		}
		public static String extractEmail(String includedPre){
			String[] div=includedPre.split(":");
			return div[1];
		}
}
